package org.example.rowing.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.example.rowing.controller.util.FormatterUtil;
import org.example.rowing.domain.Team;
import org.example.rowing.enums.BoatTypeEnum;
import org.example.rowing.enums.GenderEnum;
import org.example.rowing.service.EventInfoService;
import org.example.rowing.service.TeamInfoService;
import org.example.rowing.vo.EventVO;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.TeamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class UpdateTeamController {
    @FXML
    public TextField teamNameField;
    @FXML
    public TextField countryField;
    @FXML
    public TextField coachField;
    @FXML
    public TextField infoField;
    @FXML
    public ComboBox<String> eventNameField;
    @FXML
    private Label popupTitle;
    private TeamVO teamToEdit;
    private Map<String, Long> eventNameToIdMap = new HashMap<>();

    @Autowired
    private TeamInfoService teamInfoService;
    @Autowired
    private EventInfoService eventInfoService;


    // 用于在弹出框初始化时设置事件信息（用于编辑）
    public void initialize(TeamVO team) {
        if (team != null) {
            this.teamToEdit = team;
            popupTitle.setText("修改团队信息");
            teamNameField.setText(team.getName());
            countryField.setText(team.getCountry());
            coachField.setText(team.getCoachName());
            infoField.setText(team.getInfo());

            loadEventNames();
            eventNameField.setValue(team.getEventVO().getName());
        } else {
            popupTitle.setText("添加团队信息");
            loadEventNames();
            this.teamToEdit = null;
        }
    }

    private void loadEventNames() {
        List<EventVO> eventVOS = eventInfoService.showEventsByType(BoatTypeEnum.ROWING);
        List<String> eventNames = new ArrayList<>();
        eventNameToIdMap.clear();
        for (EventVO eventVO : eventVOS) {
            eventNames.add(eventVO.getName());
            eventNameToIdMap.put(eventVO.getName(), eventVO.getId());
        }
        eventNameField.setItems(FXCollections.observableArrayList(eventNames));
    }

    // 保存赛事
    @FXML
    private void handleSaveEvent() {
        String teamName = teamNameField.getText();
        String country = countryField.getText();
        String coach = coachField.getText();
        String info = infoField.getText();
        String eventName = eventNameField.getValue();

        if (eventName == null || teamName.isEmpty() || country.isEmpty() || coach.isEmpty()) {
            log.warn("请填写完整的比赛信息");
            showErrorDialog("请填写完整的比赛信息");
            return;
        }

        Long eventId = eventNameToIdMap.get(eventName);
        if (eventId == null) {
            log.error("赛事信息未找到，无法保存该信息");
            showErrorDialog("赛事信息未找到");
            return;
        }

        Team team = new Team(null, eventId, teamName, country, coach, info, null);

        if (teamToEdit == null) {
            // 添加赛事
            log.info("添加新比赛: {}", team);
        } else {
            // 修改赛事
            team.setId(teamToEdit.getId());
            log.info("修改比赛: {}", team);
        }
        teamInfoService.saveOrUpdateTeam(team);

        // 关闭弹窗
        closeWindow();
    }

    // 取消操作，关闭弹出框
    @FXML
    private void handleCancel() {
        closeWindow();
    }

    private void closeWindow() {
        Stage stage = (Stage) eventNameField.getScene().getWindow();
        stage.close();
    }

    // 弹出错误提示框
    private void showErrorDialog(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("输入错误");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}
